package com.yunshipei.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.MessageHistoryModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import io.rong.imkit.utils.TimeUtils;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;
import io.rong.message.TextMessage;

/**
 * Created by cike on 16/6/22.
 */
public class MessageHistoryAdapter extends android.widget.BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<MessageHistoryModel> list = null;
    private MessageHistoryModel data;
    private String keyStr = null;
    private String contents = "";

    public MessageHistoryAdapter(Context context) {
        super();
        mContext = context;
        mInflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void clear() {
        if (list != null)
            list.clear();
    }

    public void addData(MessageHistoryModel messageHistoryModel) {
        list.add(messageHistoryModel);
        this.notifyDataSetChanged();
    }

    public void addDatas(Collection<MessageHistoryModel> t) {
        if (list == null)
            list = new ArrayList<>();
        list.addAll(t);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list != null)
            return list.size();
        return 0;
    }

    @Override
    public MessageHistoryModel getItem(int position) {
        if (list == null)
            return null;
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SpannableString content = null;
        ViewHolder holder = null;
        UserInfo userInfo = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.message_history_item, null);
            holder.mImageView = (ImageView) convertView.findViewById(R.id.avatar);
            holder.title = (TextView) convertView.findViewById(R.id.conversation_id);
            holder.content = (TextView) convertView.findViewById(R.id.history_content);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        data = this.list.get(position);
        holder.title.setText(data.getTitle());
        holder.time.setText(TimeUtils.formatData(data.getMessage().getReceivedTime()));
        MessageContent messageContent = data.getMessage().getContent();
        if (messageContent instanceof TextMessage) {
            userInfo = ((TextMessage) messageContent).getUserInfo();
            contents = (userInfo == null ? "" : userInfo.getName() + ":") + ((TextMessage) messageContent).getContent();
            content = new SpannableString(contents);
            if (keyStr != null) {
                content.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.color_systemprompt)), contents.indexOf(keyStr), contents.indexOf(keyStr) + keyStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.content.setText(content);
            } else {
                holder.content.setText(((TextMessage) messageContent).getUserInfo().getName() + ":" + contents);
            }
        }
        Glide.with((Activity) mContext).load(data.getAvator()).placeholder(R.drawable.group).into(holder.mImageView);
        return convertView;
    }

    public void setKey(String str) {
        this.keyStr = str;
    }

    class ViewHolder {
        ImageView mImageView;
        TextView title, time, content;
    }
}
